package com.mybatisplus.mp.controller;

import com.mybatisplus.mp.mapper.UserMapper;
import com.mybatisplus.mp.pojo.User;
import com.mybatisplus.mp.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(description = "mybatisPlus接口demo")
//@CacheConfig(cacheNames = "coffee")
public class UserController {
    /* @Autowired
     private RedisTemplate redisTemplate;*/
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "获取用户列表", notes = "获取用户列表notes")
    @GetMapping("/get/{id}")
    public User get(@PathVariable("id") Integer id) {
        User user = userService.get(id);
        return user;
//        redisTemplate.opsForValue().set("mj","kdkd");

    }


    //提交多个相同对象1
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表notes")
    @PostMapping("/send")
    public void send(@RequestBody User users) {
//        users.getUsers().forEach(System.out::println);
        System.out.println(users);
    }

    //提交多个相同对象2
    @PostMapping("/send2")
    public void send2(@RequestBody @Valid List<User> users) {
        users.forEach(System.out::println);
    }

    //提交多个相同对象2
    @PostMapping("/eeee/{ff}")
    public void send2(@PathVariable("ff") String ff) {
        System.out.println(ff);
    }

    @GetMapping("/")
    public String hello(HttpServletResponse servletResponse) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        request.setAttribute("token", "dsafafaf");
        servletResponse.setHeader("token", "dsafafafa");
        return "ok";
    }

    @GetMapping("/mysqlTest")
    public Object test() {
        User user = ((User) userService.test());
        System.out.println(user);
        return user;
    }

    @GetMapping("/mysqlTest2")
    public void test2() throws InterruptedException {
        userService.test2();
    }

    @GetMapping("/versiontest")
    public String versiont()  {
        String s = userService.versionTest();
        return s;
    }

}
